package models

// 定义数据库连接名称常量
const dbName = "tenders" // 假设使用 db_config1 作为固定的数据库连接

// 商业险索引
type Shangyexian struct {
	ID             int     `gorm:"primaryKey;autoIncrement"`
	MasterID       int     `gorm:"not null;default:0"`
	CustomerName   string  `gorm:"size:16;not null;default:''"`
	CustomerIDCard string  `gorm:"size:18;not null;default:''"`
	CustomerPost   string  `gorm:"size:30;not null;default:''"`
	CustomerUID    int     `gorm:"not null;default:0"`
	Scheme         string  `gorm:"size:60;not null;default:''"`
	Expense        float64 `gorm:"type:decimal(18,2);not null;default:0.00"`
	TimeLimit      string  `gorm:"size:30;not null;default:''"`
	PaymentCompany string  `gorm:"size:80;not null;default:''"`
	PaymentObject  string  `gorm:"size:80;not null;default:''"`
	OutlayProject  string  `gorm:"size:30;not null;default:''"`
	BusinessType   string  `gorm:"size:30;not null;default:''"`
	ServiceName    string  `gorm:"size:30;not null;default:''"`
	CompanyID      int     `gorm:"not null;default:0"`
	CompanyName    string  `gorm:"size:80;not null;default:''"`
	BelongTime     uint    `gorm:"not null;default:0"`
	Status         bool    `gorm:"not null;default:false"`
	TypeID         uint    `gorm:"not null;default:0"`
	TypeName       string  `gorm:"size:30;not null;default:''"`
	PayStatus      bool    `gorm:"not null;default:false"`
	PayTime        int     `gorm:"not null;default:0"`
	ValidTime      int     `gorm:"not null;default:0"`
	Remarks        string  `gorm:"size:100;not null;default:''"`
	JituanID       uint    `gorm:"not null;default:0"`
	CreaterID      int     `gorm:"not null;default:0"`
	CreateTime     int     `gorm:"not null;default:0"`
	UpdateTime     int     `gorm:"not null;default:0"`
	DeleteTime     int     `gorm:"not null;default:0"`
	Database       string  `gorm:"-"`
}

// TableName 定义表名前缀
func (Shangyexian) TableName() string {
	return "daijun_shangyexian"
}

// SetDB 设置数据库连接名称
func (u *Shangyexian) SetDB(dbName string) {
	u.Database = dbName
}

// GetShangyexianInfo 通过ID获取商业险信息
func GetShangyexianInfo(id int) (shangyexian Shangyexian, err error) {
	err = DBs[dbName].First(&shangyexian, id).Error
	return shangyexian, err
}

// GetShangyexians 获取商业险列表
func GetShangyexians(pageNum int, pageSize int, maps interface{}) (shangyexians []Shangyexian) {
	DBs[dbName].Where(maps).Offset(pageNum).Limit(pageSize).Find(&shangyexians)
	return shangyexians
}

// ShangyexianList 获取商业险列表
func ShangyexianList(pageNum int, pageSize int, maps interface{}) (shangyexians []Shangyexian) {
	err := DBs[dbName].Where(maps).Offset(pageNum).Limit(pageSize).Find(&shangyexians).Error
	if err != nil {
		return nil
	}
	return shangyexians
}

// GetAllShangyexian 获取当前表的所有数据
func GetAllShangyexian() (shangyexians []Shangyexian, err error) {
	err = DBs[dbName].Find(&shangyexians).Error
	return shangyexians, err
}

// GetShangyexian 通过ID获取商业险信息
func GetShangyexian(id int) (shangyexian Shangyexian, err error) {
	err = DBs[dbName].First(&shangyexian, id).Error
	return shangyexian, err
}

// GetShangyexianTotal 获取商业险总数
func GetShangyexianTotal(maps interface{}) (count int, err error) {
	var total int64
	result := DBs[dbName].Model(&Shangyexian{}).Where(maps).Count(&total)
	if result.Error != nil {
		return 0, result.Error
	}
	return int(total), nil
}

// ExistShangyexianByMaps 判断商业险是否存在
func ExistShangyexianByMaps(maps interface{}) bool {
	var shangyexian Shangyexian
	DBs[dbName].Select("id").Where(maps).First(&shangyexian)
	return shangyexian.ID > 0
}

// ShangyexianExistTagByID 通过ID判断商业险是否存在
func ShangyexianExistTagByID(id int) bool {
	var shangyexian Shangyexian
	DBs[dbName].Select("id").Where("id = ?", id).First(&shangyexian)
	return shangyexian.ID > 0
}

// DeleteShangyexian 删除商业险
func DeleteShangyexian(maps interface{}) (bool, error) {
	if err := DBs[dbName].Where(maps).Delete(&Shangyexian{}).Error; err != nil {
		return false, err
	}
	return true, nil
}

// EditShangyexian 编辑商业险
func EditShangyexian(id int, data interface{}) (bool, error) {
	if err := DBs[dbName].Model(&Shangyexian{}).Where("id = ?", id).Updates(data).Error; err != nil {
		return false, err
	}
	return true, nil
}
